package com.tedu.egweibo2.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.tedu.egweibo2.base.response.JsonResult;
import com.tedu.egweibo2.base.response.StatusCode;
import com.tedu.egweibo2.mapper.WeiboMapper;
import com.tedu.egweibo2.pojo.dto.WeiboAddParam;
import com.tedu.egweibo2.pojo.dto.WeiboUpdateParam;
import com.tedu.egweibo2.pojo.entity.Weibo;
import com.tedu.egweibo2.pojo.vo.UserVO;
import com.tedu.egweibo2.pojo.vo.WeiboDetailVO;
import com.tedu.egweibo2.pojo.vo.WeiboIndexVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Slf4j
@Validated
@RestController
@RequestMapping("/v1/weibo/")
@Api(tags = "02.微博模块")
public class WeiboController {
    @Autowired
    public WeiboMapper weiboMapper;

    @PostMapping("insert")
    @ApiOperation(value = "发布微博")
    @ApiOperationSupport(order = 10)
    public JsonResult insert(@RequestBody WeiboAddParam weiboAddParam, @ApiIgnore HttpSession session) {
        log.debug("" + weiboAddParam);
        UserVO userVO = (UserVO) session.getAttribute("user");

        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboAddParam, weibo);
        weibo.setCreateTime(new Date());
        weibo.setUpdateTime(new Date());
        weibo.setUserId(userVO.getId());
        weibo.setIsDelete(0);

        log.debug("" + weibo);
        int count = weiboMapper.insertWeibo(weibo);
        return count > 0 ? JsonResult.ok() : new JsonResult(StatusCode.OPERATION_FAILED);
    }

    @GetMapping("selectIndex")
    @ApiOperation(value = "微博首页列表")
    @ApiOperationSupport(order = 20)
    public JsonResult selectIndex() {
        List<WeiboIndexVO> weiboIndexVOList = weiboMapper.selectWeiboAll();
        return weiboIndexVOList != null ? JsonResult.ok(weiboIndexVOList) : new JsonResult(StatusCode.OPERATION_FAILED);
    }

    @GetMapping("selectById")
    @ApiOperation(value = "微博详情页")
    @ApiOperationSupport(order = 30)
    @ApiImplicitParam(name = "id", value = "微博编号", required = true)
    public JsonResult selectById(@Range(min = 200, max = 500, message = "微博编号必须在200-500之间") @RequestParam Long id) {
        log.debug("微博id: " + id);
        WeiboDetailVO weiboDetailVO = weiboMapper.selectWeiboById(id);
        return weiboDetailVO != null ? JsonResult.ok(weiboDetailVO) : new JsonResult(StatusCode.OPERATION_FAILED);
    }

    @PostMapping("update")
    @ApiOperation(value = "修改微博")
    @ApiOperationSupport(order = 40)
    public JsonResult update(@RequestBody WeiboUpdateParam weiboUpdateParam, @ApiIgnore HttpSession session) {
        log.debug("" + weiboUpdateParam);
        UserVO userVO = (UserVO) session.getAttribute("user");

        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboUpdateParam, weibo);
        weibo.setUpdateTime(new Date());

        log.debug("" + weibo);
        int count = weiboMapper.updateWeibo(weibo);
        return count > 0 ? JsonResult.ok() : new JsonResult(StatusCode.OPERATION_FAILED);
    }

    /**
     * 删除微博，伪删除
     *
     * @param isDelete
     * @param id
     * @param session
     * @return
     */
    @PostMapping("delete")
    @ApiOperation(value = "删除微博")
    @ApiOperationSupport(order = 50)
    @ApiImplicitParams(
            value = {
                    @ApiImplicitParam(name = "isDelete", value = "是否确认删除", example = "输入\"确认删除\"，即可完成删除"),
                    @ApiImplicitParam(name = "id", value = "微博编号", required = true)
            }
    )
    public JsonResult delete(String isDelete, Long id, @ApiIgnore HttpSession session) {
        log.debug(isDelete + ",id: " + id);
        UserVO userVO = (UserVO) session.getAttribute("user");

        int count = 0;
        Long userId = userVO.getId();
        if ("确认删除".equals(isDelete.trim())) {
            count = weiboMapper.deleteWeiboById(id, userId);
        }
        return count > 0 ? JsonResult.ok() : new JsonResult(StatusCode.OPERATION_FAILED);
    }

}
